/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ejb;

import datamodel.Post;
import datamodel.Ticket;
import java.util.List;
import javax.annotation.security.RolesAllowed;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class PostManager {

    @PersistenceContext
    private EntityManager em;

    @RolesAllowed({"SystemAdmin","SystemCustomer"})
    public List<Post> getAllPosts() {
        return em.createQuery("SELECT p FROM Post p").getResultList();
    }
    
    @RolesAllowed({"SystemAdmin","SystemCustomer"})
    public List<Post> getAllPostsOf(Ticket t) {
        return em.createQuery("SELECT p FROM Post p WHERE p.inTicket=:ticket ORDER BY p.id DESC").setParameter("ticket", t).getResultList();
    }
    
    @RolesAllowed({"SystemAdmin","SystemCustomer"})
    public void savePost(Post temp) {
        em.persist(temp);
        em.flush();
    }

    @RolesAllowed({"SystemAdmin","SystemCustomer"})
    public Post getPost(int id) {
        return em.find(Post.class, id);
    }
    
    @RolesAllowed({"SystemAdmin","SystemCustomer"})
    public void remove(Post temp) {
        em.remove(temp);
        em.flush();
    }
}
